<?php
require_once 'common.php';

/**
 * 个人消息模型
 * @author wangjiajun
 *
 */
class OaPersonalMessageModel extends OpModel
{
    // 阅读状态
    const MESSAGE_UNREAD = 0;    // 未读
    const MESSAGE_READED = 1;    // 已读
    
    private $_personalMessageTable;
    
    public static function getConstantValueByName($name, $default = 0)
    {
        return self::_getConstByName($name, '', $default, 'OaPersonalMessageModel');
    }
    
    public function __construct($logger = null)
    {
        if (is_null($logger)) {
            $logger = getOaModelLogger();
        }
        parent::__construct($logger);
        $dbConfig = Oa_Util_Config::getSingleDbConfig(OA_WEB_DB_CLUSTER_ID);
        $db = OpDbModel::getInstance($dbConfig, $logger);
        $this->_personalMessageTable = new OpTableModel($db, 'personal_message', $logger);
    }
    
    /**
     * 发送一条消息
     * @param int $developerId
     * @param string $title
     * @param string $content
     * @param int $sender
     * @param int $companyId 跟消息相关的公司
     * @param array $row
     * @return int|boolean
     */
    public function sendMessage($developerId, $title, $content, $sender, $companyId = 0, $row = array())
    {
        $row = array_merge(array(
            'developer_id' => $developerId,
            'sender' => $sender,
            'title' => $title,
            'content' => $content,
            'is_read' => self::MESSAGE_UNREAD,
            'company_id' => $companyId,
            'create_time' => date('Y-m-d H:i:s')
        ), $row);
        return $this->_personalMessageTable->insert($row);
    }
    
    /**
     * 查询消息
     * @param int $id
     * @return array|null|boolean
     */
    public function getMessage($id)
    {
        return $this->_personalMessageTable->getRow(array(
            'id' => $id
        ));
    }
    
    /**
     * 查询消息列表
     * @param array $where
     * @param int $offset
     * @param int $count
     * @param string $sort
     * @param array $otherWhere
     * @return array|boolean
     */
    public function getMessageList($where, $offset = 0, $count = null, $sort = null, 
        $otherWhere = null)
    {
        return $this->_personalMessageTable->get($where, $offset, $count, $sort, $otherWhere);
    }
    
    /**
     * 查询消息列表数
     * @param array $where
     * @param array $otherWhere
     * @return int|boolean
     */
    public function getMessageListCount($where, $otherWhere = null)
    {
        return $this->_personalMessageTable->getCount($where, $otherWhere);
    }
    
    /**
     * 修改消息
     * @param int $id
     * @param array $row
     * @return boolean
     */
    public function modifyMessage($id, $row)
    {
        return $this->_personalMessageTable->update(array(
            'id' => $id
        ), $row);
    }
    
    /**
     * 设置消息已读
     * @param int $id
     * @return boolean
     */
    public function setMessageReaded($id)
    {
        return $this->modifyMessage($id, array(
            'is_read' => self::MESSAGE_READED
        ));
    }
    
    /**
     * 删除消息
     * @param int $id
     * @return boolean
     */
    public function deleteMessage($id)
    {
        return $this->_personalMessageTable->delete(array(
            'id' => $id
        ));
    }
    
    /**
     * 返回底层的个人短消息Table对象
     * @return OpTableModel
     */
    public function getPersonalMessageTable()
    {
    	return $this->_personalMessageTable;
    }
}